package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 *	5736
 * http://n.cztv.com/  新蓝网
 *
 * @author KK
 *
 */
public class CztvNewsAnalyse implements AnalyseNews {
/**
 * http://www.cztv.com/s/2014/lianghui/dbjy/2014/01/2014-01-174237859.htm
 * http://n.cztv.com/ws/nsf/video/2014/01/2014-01-034215722.htm
 * http://www.cztv.com/s/2014/lianghui/lhjxs/2014/01/2014-01-174237720.htm
 */
	private static final String[] regex={
		/*
		*		[0-9]*
		*       [a-z]*
		*		[a-z0-9]*
		*       [a-zA-Z0-9]*
		*/

//		"http://[a-z]*.cztv.com/[a-z]*/[0-9]*/[0-9]*/[0-9]*-[0-9]*-[0-9]*.htm",
//		"http://[a-z]*.cztv.com/[a-z0-9].*/[a-z0-9]*/[a-z0-9]*/[a-z0-9]*/[0-9]{4}/[0-9]{1,2}/[0-9]*-[0-9]*-[0-9]*.html?",
//		"http://[a-z]*.cztv.com/[a-z0-9].*/[a-z0-9]*/[a-z0-9]*/[0-9]{4}/[0-9]{1,2}/[0-9]*-[0-9]*-[0-9]*.html?",
//		"http://[a-z]*.cztv.com/[a-z0-9]*/[a-z0-9]*/[0-9]{4}/[0-9]{1,2}/[0-9]*-[0-9]*-[0-9]*.html?"
			"http://n.cztv.com/[a-z0-9]+/[0-9]+.html",
			"http://[a-z]+.cztv.com/[a-z\\/]+[0-9\\/]+[0-9\\-]+.html"
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		news.setUrl(url);

		title =doc.select("title").text();
		content=HtmlCleaner.getContentHtml(url,doc.select("div#zoom"));
		if (StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.txt_info.cl"));
		}
		if (StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.summary"));
		}
		if (StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.de_nrb"));
		}

		/*
		 * 作者
		 */
		author=JSoupUtils.matchAuthor(doc, "来源：");
		if(StringUtils.isBlank(author)){
			author=doc.select("div.pubinfo > span.where").text();
		}

		date=DateUtils.matchDate(doc.select("div.publish_info").text());
		if(date == null){
			date=DateUtils.matchDate(doc.select("div.pubinfo").text());
		}
		if(date == null){
			date=DateUtils.matchDate(doc.select("div.de_box04 div.lyz").text());
		}
		if (date==null){
			date=DateUtils.matchDate(url);
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		news.setType(1);
		return news;
	}
	public static void main(String[] args) {
		String url="http://n.cztv.com/news2014/1176498.html";
		CztvNewsAnalyse analyse=new CztvNewsAnalyse();
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
